package day16;

public class TestRunnableV2 {
    public static void main(String[] args) {
        TicketR2 target = new TicketR2();
        Thread t1 = new Thread(target);
        Thread t2 = new Thread(target);
        Thread t3 = new Thread(target);
        Thread t4 = new Thread(target);
        t1.start();
        t2.start();
        t3.start();
        t4.start();

    }
}

class TicketR2 implements Runnable {
    int tickets = 100;
    Object o = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (o) {
                if (tickets > 0) {
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    System.out.println(Thread.currentThread().getName() + "=" + tickets--);

                }
                if (tickets <= 0) break;
            }
        }
    }
}